#ifndef __BSP_PTHREAD__H__
#define __BSP_PTHREAD__H__

#include "config.h"


#include "bsp_uart.h"
#include "bsp_video.h"
#include "mpp_frame.h"
//p_thd ctl cmd (register、idle、pause、running、shutdown、terminate)
typedef enum {
    CMD_CTL_REGISTER,       //注册新线程
    CMD_CTL_IDLE,           //线程空闲
    CMD_CTL_PAUSE,          //线程暂停
    CMD_CTL_RUNNING,        //线程运行
    CMD_CTL_SHUTDOWN,       //请求正常退出，资源会被释放
    CMD_CTL_TERMINATE       //强制终止，资源可能被释放
}ThreadControlCmd;
//p_thd status cmd (idle、pause、running、shutdown、terminate、dead)
typedef enum {
    CMD_STATE_IDLE,          // 空闲
    CMD_STATE_PAUSE,         // 暂停
    CMD_STATE_RUNNING,       // 运行
    CMD_STATE_SHUTDOWN,      // 正常关闭
    CMD_STATE_TERMINATE,     // 强制关闭
    CMD_STATE_DEAD           // 资源已经释放
}ThreadCmdState;
//p_thd args <uart videosub>
typedef struct {
    Video_t     *video;
    Uart_t      *uart;
}ThreadDevArgs;
//glb p_thd ctx -> pthread_xxx
typedef struct {
    pthread_t           thread_id;  // 线程ID
    ThreadCmdState      thdstate;      // 状态


    pthread_mutex_t     lock;     // 状态锁
    pthread_cond_t      cond;      // 条件变量

    ThreadDevArgs *args;   // 当前参数

    // 线程状态控制cmd
    ThreadControlCmd    thdctlcmd;


    volatile bool       thdstop_flag;  // 停止标志
    volatile bool       thdcmdhandle_flag;  // 命令处理标志
    volatile bool       thdresfree_flag;  // 资源释放标志

}ThreadDevContext;
#ifdef __cplusplus
extern "C" {
#endif

void signal_thread_to_exit(pthread_t *thread_id, volatile int *exit_flag);
void *thd_read_mcu(void *arg);
void *thd_send_mcu(void *arg);
void *laser_polling_thd(void *arg);
void *Uarts_SelectFDs_thd(void *arg);
void *thd_timeout_5min(void *arg);
#ifdef __cplusplus
}
#endif

#endif